Q and A - I
PHP stands for Hypertext Preprocessor. It is a server-side scripting language used to develop dynamic and interactive web pages.
<?php to start and ?> to end.
Using echo or print. Example: echo "Hello World!";
Both output text, but print returns a value (1) and can be used in expressions, while echo cannot.
Using // or #. Example: // This is a comment
Variables store data values and start with $. Example: $name = "Dinesh";
Using the . (dot) operator. Example: echo "Hello " . "World!";
String, Integer, Float, Boolean, Array, Object, NULL, and Resource.
Using define("NAME", "value");
== checks value equality, while === checks both value and data type.
gettype($variable);
strlen("Hello");
An array that uses named keys instead of numeric indexes. Example: $age = ["Ram" => 25, "Sita" => 23];
Using include 'file.php'; or require 'file.php';
If a file is missing, include shows a warning and continues execution; require stops execution with a fatal error.
$conn = mysqli_connect("localhost", "root", "", "test");
if (!$conn) { die("Connection failed: " . mysqli_connect_error()); }
$result = mysqli_query($conn, "SELECT * FROM users");
while($row = mysqli_fetch_assoc($result)) {
echo $row['name'];
}
Checks if a variable is set and not null.
Checks whether a variable is empty.
session_start();
session_destroy();
A small piece of data stored on the client's browser.
setcookie("user", "Dinesh", time()+3600);